package com.itheima.test;

import com.itheima.domain.Hero;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author 王加鑫
 * @Date 2021/4/29 15:13
 * @Version 1.0
 */
public class HeroManger {

    public static void main(String[] args) {
        //初始化一个集合容器,管理系统中所有英雄的信息
        ArrayList<Hero> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        lo:while (true){
            System.out.println("----------欢迎来到英雄联盟----------");
            System.out.println("1 添加英雄");
            System.out.println("2 删除英雄");
            System.out.println("3 修改英雄");
            System.out.println("4 查找英雄");
            System.out.println("5 退出系统");
            System.out.println("请输入您的操作:");
            String s = sc.next();
            switch (s){
                case "1":
                    addHero(list);
                    break;
                case "2":
                    deleteHero(list);
                    break;
                case "3":
                    updateHero(list);
                    break;
                case "4":
                    queryHero(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }

    private static void updateHero(ArrayList<Hero> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的英雄id:");
        int id = sc.nextInt();
        if(getIndex(list,id) == -1){
            System.out.println("您输入的id有误,请重试!");
        }else {
            System.out.println("请输入您要修改的英雄名字:");
            String name = sc.next();
            System.out.println("请输入您要修改的英雄年龄:");
            int age = sc.nextInt();
            System.out.println("请输入您要修改的英雄生日:");
            String birth = sc.next();
            Hero hero = new Hero(id,name,age,birth);
            list.set(getIndex(list,id),hero);
            System.out.println("修改成功");

        }
    }

    private static void deleteHero(ArrayList<Hero> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的英雄id:");
        int id = sc.nextInt();
        int index = getIndex(list,id);
        if(index != -1){
            list.remove(index);
            System.out.println("删除成功!");
        }else {
            System.out.println("您要删除的英雄不存在,请重试!");
        }
    }

    /*
    添加英雄
     */
    public static void addHero(ArrayList<Hero> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入英雄id:");
        while (true){
            int id = scanner.nextInt();
            int index = getIndex(list,id);
            if(index != -1){
                System.out.println("id重复,请重新输入:");

            }else {
                System.out.println("请输入英雄姓名:");
                String name = scanner.next();
                System.out.println("请输入英雄年龄:");
                int age = scanner.nextInt();
                System.out.println("请输入英雄生日:");
                String birth = scanner.next();
                //将键盘录入的封装为学生对象
                Hero hero = new Hero(id,name,age,birth);
                //将对象封装进集合
                list.add(hero);
                System.out.println("添加成功!");
                break;
            }
        }
    }

    /*
    查找英雄
     */
    public static void queryHero(ArrayList<Hero> list) {
        /*
        先判断集合是否为空
         */
        if(list.size() == 0){
            System.out.println("系统数据为空!请添加数据后再查找!");
            return;
        }
        System.out.println("英雄id\t\t英雄姓名\t\t英雄年龄\t\t英雄生日");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).gethId() + "\t\t\t" + list.get(i).gethName()
                    + "\t\t" + list.get(i).gethAge() + "\t\t\t" + list.get(i).getBirthday());
        }
    }

    /*
    查找集合中指定位置的索引
     */
    public static int getIndex(ArrayList<Hero> list,int id){
        //标记位思想,假设索引不存在
        int index = -1;
        //遍历集合,比较索引
        for (int i = 0; i < list.size(); i++) {
            //如果查找的索引在集合中存在,那么索引就是对应查找出来的索引
            if(id == list.get(i).gethId()){
                index = i;
            }
        }
        return index;

    }
}
